# Verify conditions
if [ ! -z $UID ] && [ $UID -ne 0 ]; then
    echo Superuser privileges are required to run this script.
    exit 1
fi

ping -c 1 -W 1 8.8.8.8 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    ping -c 1 -W 1 www.baidu.com > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo Network unreachable.
        exit 1
    fi
fi

if [ -d "/usr/share/dotnet" ]; then
    echo .net environment conflicts. Please remove the environments installed by 'apt install'.
    exit 1
fi

# Remove files of legacy installation
if [ -d "/opt/dotnet" ]; then
    rm -rf /opt/dotnet
    echo /opt/dotnet removed.
fi

if [ ! -z `cat /etc/environment | grep DOTNET_ROOT=` ]; then
    sed -i '/^DOTNET_ROOT=/d' /etc/environment
    echo /etc/environment reverted.
fi

if [ -f "/etc/profile.d/dotnet-core.sh" ]; then
    rm -f /etc/profile.d/dotnet-core.sh
    echo /etc/profile.d/dotnet-core.sh removed.
fi

if [ -f "/etc/profile.d/dotnet.sh" ]; then
    rm -f /etc/profile.d/dotnet.sh
    echo /etc/profile.d/dotnet.sh removed.
fi

# Remove install lock
if [ -f "/var/lib/apt/lists/lock" ] || [ -f "/var/lib/dpkg/lock" ] || [ -f "/var/lib/dpkg/lock-frontend" ] || [ -f "/var/cache/apt/archives/lock" ]; then
    echo The apt system may be locked. Trying to unlock.
    apt update
    rm -f /var/lib/apt/lists/lock
    rm -f /var/lib/dpkg/lock
    rm -f /var/lib/dpkg/lock-frontend
    rm -f /var/cache/apt/archives/lock
fi

# Update apt
apt update

# Install software and tools
apt install -y build-essential glade dos2unix git unrar unzip libsqlite3-0 libwebkit2gtk-4.0-37 libpython3-dev python3-pip ffmpeg

# Install .net SDK
if [ ! -d "/usr/share/dotnet-manually-installed/sdk/8.0.203" ] || [ ! -d "/usr/share/dotnet-manually-installed/sdk/6.0.300" ]; then
    mkdir -p /usr/share/dotnet-manually-installed
    if [ ! -d "/usr/share/dotnet-manually-installed/sdk/8.0.203" ]; then
        DOTNETCORE_PACKAGE_8_0=dotnet-sdk-8.0.203-linux-arm64.tar.gz
        if [ ! -f $DOTNETCORE_PACKAGE_8_0 ]; then
            echo $DOTNETCORE_PACKAGE_8_0 not found.
            exit 1
        fi
        tar zxvf $DOTNETCORE_PACKAGE_8_0 -C /usr/share/dotnet-manually-installed
    fi
    if [ ! -d "/usr/share/dotnet-manually-installed/sdk/6.0.300" ]; then
        DOTNETCORE_PACKAGE_6_0=dotnet-sdk-6.0.300-linux-arm64.tar.gz
        if [ ! -f $DOTNETCORE_PACKAGE_6_0 ]; then
            echo $DOTNETCORE_PACKAGE_6_0 not found.
            exit 1
        fi
        tar zxvf $DOTNETCORE_PACKAGE_6_0 -C /usr/share/dotnet-manually-installed
    fi
    chmod 777 -R /usr/share/dotnet-manually-installed

    apt install -y libc6 libgcc1 libgcc-s1 libgssapi-krb5-2 libicu70 liblttng-ust1 libssl3 libstdc++6 libunwind8 zlib1g libgdiplus
fi

if [ -z `cat /etc/environment | grep DOTNET_ROOT=` ]; then
    echo 'DOTNET_ROOT=/usr/share/dotnet-manually-installed' | tee -a /etc/environment
fi

if [ ! -f "/etc/profile.d/dotnet-manually-installed.sh" ]; then
    mkdir -p /etc/profile.d
    echo -e 'export PATH=$PATH:$DOTNET_ROOT:$HOME/.dotnet/tools' > /etc/profile.d/dotnet-manually-installed.sh
fi

if [ ! -f "/etc/environment.d/dotnet-asc.conf" ]; then
    mkdir -p /etc/environment.d
    echo 'COMPlus_EnableAlternateStackCheck=1' > /etc/environment.d/dotnet-asc.conf
fi

# Install OpenCV
OPENCV_PACKAGE=OpenCV410-ArmUbuntu2204.zip
if [ -f $OPENCV_PACKAGE ]; then
    apt install -y libopenexr25 libdc1394-25
    unzip -o -d / ./$OPENCV_PACKAGE
fi

# Install VSCode
if [ ! -f "/usr/share/code/bin/code" ]; then
    VSCODE_PACKAGE=code_1.101.2-1750797955_arm64.deb
    if [ ! -f $VSCODE_PACKAGE ]; then
        echo $VSCODE_PACKAGE not found.
        exit 1
    fi

    dpkg -i $VSCODE_PACKAGE
    apt install -yf
fi

# Install Clang
if [ ! -d "/opt/clang-15" ]; then
    CLANG_PACKAGE=clang-15-arm-linux.zip
    if [ ! -f $CLANG_PACKAGE ]; then
        echo $CLANG_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt ./$CLANG_PACKAGE
fi

# Install XTools
if [ ! -d "/opt/xtools-uni/x86-windows10" ]; then
    UNI_X86_WINDOWS10_PACKAGE=20230521-x86-windows10.zip
    if [ ! -f $UNI_X86_WINDOWS10_PACKAGE ]; then
        echo $UNI_X86_WINDOWS10_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt/xtools-uni ./$UNI_X86_WINDOWS10_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/x86-ubuntu2204" ]; then
    UNI_X86_UBUNTU2204_PACKAGE=20230602-x86-ubuntu2204.zip
    if [ ! -f $UNI_X86_UBUNTU2204_PACKAGE ]; then
        echo $UNI_X86_UBUNTU2204_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt/xtools-uni ./$UNI_X86_UBUNTU2204_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/arm-ubuntu2204" ]; then
    UNI_ARM_UBUNTU2204_PACKAGE=20230602-arm-ubuntu2204.zip
    if [ ! -f $UNI_ARM_UBUNTU2204_PACKAGE ]; then
        echo $UNI_ARM_UBUNTU2204_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt/xtools-uni ./$UNI_ARM_UBUNTU2204_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/x86-macos1013" ]; then
    UNI_X86_MACOS1013_PACKAGE=20230531-x86-macos1013.zip
    if [ ! -f $UNI_X86_MACOS1013_PACKAGE ]; then
        echo $UNI_X86_MACOS1013_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt/xtools-uni ./$UNI_X86_MACOS1013_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/arm-macos1203" ]; then
    UNI_ARM_MACOS1203_PACKAGE=20230522-arm-macos1203.zip
    if [ ! -f $UNI_ARM_MACOS1203_PACKAGE ]; then
        echo $UNI_ARM_MACOS1203_PACKAGE not found.
        exit 1
    fi

    unzip -d /opt/xtools-uni ./$UNI_ARM_MACOS1203_PACKAGE
fi

# Install free .NET debugger
if [ ! -d "/opt/netcoredbg" ]; then
    NETCOREDBG_PACKAGE=netcoredbg-3.1.2-linux-arm64.tar.gz
    if [ ! -f $NETCOREDBG_PACKAGE ]; then
        echo $NETCOREDBG_PACKAGE not found.
        exit 1
    fi

    tar -vxzf ./$NETCOREDBG_PACKAGE -C /opt

    if [ ! -f "/etc/profile.d/netcoredbg.sh" ]; then
        mkdir -p /etc/profile.d
        echo -e 'export PATH=$PATH:/opt/netcoredbg' > /etc/profile.d/netcoredbg.sh
    fi
fi

echo Done.
